<?php

/*
 * 交易信息模块
 * and open the template in the editor.
 * 需要加入区域模块
 */
class TransactionAction extends CommonAction {  
    function _filter(&$map){
		if ($_POST['name']=='请输入关键字')$_POST['name']='';
		$map['name'] = array('like',"%".$_POST['name']."%");
    }
    
    public function index()
    {
        //读取系统的用户列表
        //读取系统的用户列表
     
        $zone    =   D("Zone");
	$list=$zone->where('pid=1')->field('id,name,biznum')->select();
	$tmp_date=date("Ym");
        $tmp_mon =substr($tmp_date,4,2);
        $tmp_year =substr($tmp_date,0,4);
        $this->assign('mon',$tmp_mon);
        $this->assign('year',$tmp_year);	

         
        $this->assign('list',$list);
        $this->display();

        return;
    }
    public function  comList()
    {
       $pId =  isset($_GET['zoneid'])?$_GET['zoneid']:1;
       $tmp_year =  isset($_GET['year'])?$_GET['year']:substr(date("Ym"),0,4);
       $tmp_mon =  isset($_GET['mon'])?$_GET['mon']:substr(date("Ym"),4,2);
       
        //读取系统的用户列表
        $zone    =   M("Zone");
        $zonename = $zone->where('id='.$pId)->getField('name');
        $company    =   D("BizrecordView");
       
       //$list=$company->where('status=1 AND (water=1 OR electricity=1) AND zoneid ='.$pId)->select();

        $where['status']=1;
        //$where['mon']=$tmp_mon;
        //$where['year']=$tmp_year;
	$where['zoneid']=$pId ;
       if (!empty($company)) {
            $this->_list($company, $where,'id',true);
        }
     
       $this->assign('mon',$tmp_mon);
       $this->assign('year',$tmp_year);
       $this->assign('zonename',$zonename); 
       //$this->assign('list',$list);
        
        $this->display();

        return;
    }
     public function insertTran(){
          $resid=  isset($_POST['resid'])?$_POST['resid']:0;
         $bizid=  isset($_POST['bizid'])?$_POST['bizid']:0;
         $plist= isset($_POST['pay_list'])?$_POST['pay_list']:"0";
         $pay_cost= isset($_POST['pay_cost'])?$_POST['pay_cost']:0;//应收费用
         $pay_cost1= isset($_POST['pay_cost1'])?$_POST['pay_cost1']:0;//实收费用
         $cp=  isset($_POST['cp'])?$_POST['cp']:0;//物业费标示位
         $ch=  isset($_POST['ch'])?$_POST['ch']:0;//取暖费标示位
          $sid=  $_POST['bizSid'];
          $data['user_id']=$_SESSION[C('USER_AUTH_KEY')];
          $data['resid']=$resid;
          $data['receivable']=$pay_cost;    //应收小数后2位
          $data['paid']=   $pay_cost1 ;       //实收小数后1位
          $data['inputdate']=Time(now);
         if($cp==1){
            $p_anum=  isset($_POST['p_anum'])?$_POST['p_anum']:0; //实际费用 应收费用小数点后3位
            $p_anum1= isset($_POST['p_anum1'])?$_POST['p_anum1']:"0"; //显示收取费用 小数点后2位
         }
         if($ch==1){
            $h_anum=  isset($_POST['h_anum'])?$_POST['h_anum']:0; //实际费用 应收费用小数点后3位
            $h_anum1= isset($_POST['h_anum1'])?$_POST['h_anum1']:"0"; //显示收取费用 小数点后2位
         }
        
         $plist=substr($plist,1);  //获得Record单据号字符串。
         $biz= D("Bizmanage");  
         
         $com = $biz->where('id='.$bizid)->find();
      
         
          $TranRecord= D("Transaction");  
          
            if (false === $TranRecord->create()) {
                $this->error($TranRecord->getError());
            }else{
                    $data['propertyid']=$propertyid;
                    $data['heatingid']=$heatingid;
                    $result= $TranRecord->add($data);
                    $tranid= $result;
                      //判断物业取暖信息。如果有就更新表
                     if($result){
                                 $propertyid =0;
                                 $p_update=0;
                                 
                                 $tmp_date=date("Ym");
                                 $tmp_year =substr($tmp_date,0,4);
                                 $tmp_mon =substr($tmp_date,4,2);         
                                 
                                 if ($cp==1){
                                        $pDate['paytype']=0;
                                        $pDate['placenum']=$com['znum'];
                                        $pDate['bizid']=$bizid;
                                        $pDate['z_price']=$com['p_price'];
                                        $pDate['mon']=6;
                                        $pDate['usertype']=0;
                                        $pDate['tranid']=$tranid;
                                        $pDate['anum']=$p_anum1;
                                        $pDate['inputdate']=Time(now);
                                        $pDate['user_id']=$_SESSION[C('USER_AUTH_KEY')];
                                        if (intval($tmp_mon)<7){
                                            $p_update= $tmp_year."07";
                                        }else{
                                            $p_update= (intval($tmp_year)+1)."01";
                                        }
                                       
                                        //得到物业的ID号
                                        $propertyid = $TranRecord->addPropertyRecord($pDate);
                                    }
                                        //判断取暖
                                        $heatingid =0;
                                        $h_update= 0;
                                            if($ch==1){
                                                $pDate['paytype']=1;
                                                $pDate['placenum']=$com['znum2'];
                                                $pDate['bizid']=$bizid;
                                                $pDate['z_price']=$com['h_price'];
                                                $pDate['mon']=4;
                                                $pDate['usertype']=0;
                                                $pDate['tranid']=$tranid;
                                                $pDate['inputdate']=Time(now);
                                                $pDate['user_id']=$_SESSION[C('USER_AUTH_KEY')];
                                                $pDate['anum']= $h_anum1;
                                                $h_update= ($tmp_year+1)."03";
                                                $heatingid = $TranRecord->addPropertyRecord($pDate);
                                            }
                                




                            //更新抄表Record记录状态 meter_record的payflag标记为交易单据

                             $TranRecord->updatePaysFlag($bizid,$result,$plist);
                            //更新biz表的pay_update 缴费最后状态，根据物业取暖更新p_update和h_update，物业取暖的最后状态
                            if($p_update!=0||$h_updat!=0){
                                $TranRecord->updatePhdate($bizid,$p_update,$h_update);
                            }
                            //更新record_statistics统计表，更新所在区域的paynum,当前月缴费计数加1
                            $TranRecord->updateStatistics($com['zoneid'],$plist);


                    }
                    
                    
                    
            }
            $ajaxdata['tran_id']=$tranid;
            $ajaxdata['propertyid']=$propertyid;
            $ajaxdata['heatingid']=$heatingid;
            $this->ajaxReturn($ajaxdata,"缴费成功！",1);
         
     }
    public function insertItem(){
          
          $resid=  isset($_POST['resid'])?$_POST['resid']:0;
          if($resid==0){
              $this->error("输入错误，表单记录号不存在！");
          }

          $pay_num= $_POST['pay_anum'];//实际收取费用，精确到角
          $pay_num1= $_POST['pay_anum1'];//应该收取费用，精确到分
          $cp=  isset($_POST['cp'])?$_POST['cp']:0;//物业费标示位
          $ch=  isset($_POST['ch'])?$_POST['ch']:0;//取暖费标示位
          $mon1=  isset($_POST['monyy'])?$_POST['monyy']:6;
          $sid=  $_POST['bizSid'];
          $data['userid']=$_SESSION[C('USER_AUTH_KEY')];
          $data['resid']=$resid;
          $data['receivable']= $pay_num1;    //应收
          $data['paid']=   $pay_num ;       //实收
          $data['inputdate']=Time(now);
          $meter=D('BizrecordView');
          
          $rs=$meter->where('resid='. $resid)->find();
          $data['recordid']=$rs['meterid'];
         $TranRecord= D("Transaction");  
            //启用事务
        $TranRecord->startTrans();
         //判断物业,如果有。更新物业信息
         $propertyid =0;
         $p_update=0;
         if ($cp==1){
                $pDate['paytype']=0;
                $pDate['placenum']=$rs['znum'];
                $pDate['bizid']=$rs['id'];
                 $pDate['z_price']=$rs['p_price'];
                 $pDate['mon']=$mon1;
                 if ($rs['p_update']!=0){
                         $tmp_mon =substr($tmp_date,4,2);
                          $tmp_year =substr($tmp_date,0,4);
                           $p_update=date("Ym", strtotime("+".$mon1." months", strtotime($tmp_date."01")));
                          
                 }else{
                         $tmp_date=date("Ym");
                          $tmp_year =substr($tmp_date,0,4);
                          $p_update= $tmp_year."12";
                 }
                //得到物业的ID号
                $propertyid = $TranRecord->addPropertyRecord($pDate);
         }
         //判断取暖
         $heatingid =0;
         $h_update= 0;
            if($ch==1){
                 $pDate['paytype']=1;
                 $pDate['placenum']=$rs['znum2'];
                 $pDate['bizid']=$rs['id'];
                 $pDate['z_price']=$rs['h_price'];
                 $pDate['mon']=4;
                 $tmp_date=date("Ym");
                 $tmp_year =substr($tmp_date,0,4);
                 $h_update= ($tmp_year+1)."03";
                 $heatingid = $TranRecord->addPropertyRecord($pDate);
            }
            if (false === $TranRecord->create()) {
                $this->error($TranRecord->getError());
            }else{
                    $data['propertyid']=$propertyid;
                    $data['heatingid']=$heatingid;
                    $result= $TranRecord->add($data);
                //逐条插入表记录
                   if($result ){

                        //更新抄表Record记录状态 meter_record的payflag标记为1

                        $TranRecord->updatePayFlag(0,0,$resid,$result);
                            //更新biz表的pay_update 缴费最后状态，根据物业取暖更新p_update和h_update，物业取暖的最后状态
                            if($p_update!=0||$h_updat!=0){
                                $TranRecord->updatePhdate($rs['id'],$p_update,$h_update);
                            }
                            //更新record_statistics统计表，更新所在区域的paynum,当前月缴费计数加1
                            $TranRecord->updateStatistics($rs['zoneid'],$rs['meterMon']);


                    }
            }
           
           if($result){
                             
               $TranRecord->commit();//成功则体积哦啊
            }else{
               $TranRecord->rollback();//不成功，回滚
            }
           $this->display();
            return;
        //保存当前数据对象
        
        
    }


    public function tranInfo(){
        //获取用户单据号查找
       $srcId =  isset($_REQUEST['resid'])?$_REQUEST['resid']:0; 
       $meter=D('BizrecordView');
       if ($srcId==0||strlen($srcId)!=10){
           $this->error("输入错误，输入单据号错误！");
       }else{
           $rs=$meter->where('resid='.$srcId)->find();
           $rnum=sizeof($rs);
            if($rnum>0){
                //有数据,
                $bizid = $rs['id'];
                //获得明细信息
                $rid=  $rs['meterid'];
               if($rs['p_update']>0){
                   $p_mon=12;
               }else{
                   $tmp_date=Date('Ym');
                   $t_mon=substr($tmp_date,4,2);
                    $p_mon=12-$t_mon;
                    $t_year=substr($tmp_date,0,4);
                    $rs['p_update']=$tmp_date;
               }                                
                $tmp_mon =substr($rs['meterMon'],4,2);
                $tmp_year =substr($rs['meterMon'],0,4);
                $this->assign('p_mon',$p_mon);
                $this->assign('mon',$tmp_mon);
                $this->assign('year',$tmp_year);	

                $this->assign('com',$rs);
                
                //显示催缴信息
                //一条记录直接显示单据内容
                   $meterItem=D('MeteritemView');
                   $rsitems=$meterItem->where('recordid='.$rid)->select();
                   $ww_num=0;
                  for($i=0;$i<sizeof($rsitems);$i++)
                    {
                    if ($rsitems[$i]['metertype']== 1){
                        $w_meterlist[]=$rsitems[$i];
                    }else{
                        $e_meterlist[]=$rsitems[$i];
                    }
                }
                $this->assign('listw',$w_meterlist);
                $this->assign('liste',$e_meterlist);
                 
            }else{
                $this->error("单据不存在！");
           
            }
            $this->display();
            return;
       }
       
       
       
       
       
       
    }
    public function tranbizInfo(){
        //获取用户单据号查找
       $bizId =  isset($_REQUEST['bizid'])?$_REQUEST['bizid']:0; 
       $meter=D('Biz');
           $rs=$meter->where('id='.$bizId)->find();
           $rnum=sizeof($rs);
            if($rnum>0){
                //有数据,
                $bizid = $rs['id'];
                //获得明细信息
                $rid=  $rs['meterid'];
                $tmp_date=Date('Ym');
                $t_mon=substr($tmp_date,4,2);//当前月份
               if($rs['p_update']>0){
                   $p_mon=6;
               }else{
                    $p_mon=12-$t_mon;
                    $t_year=substr($tmp_date,0,4);
                    $rs['p_update']=$tmp_date;
               } 
               //如果未到物业截止时间（本物业期费用已交）
               $showP=1;
               if(strtotime($rs['p_update'].'01')>strtotime($tmp_date.'01')){
                   $showP=0;
               }
               $showH=1;
               if((strtotime($rs['h_update'].'01')>strtotime($tmp_date.'01'))||(intval($t_mon)<6&&intval($t_mon)>=4)){
                   $showH=0;
               }
               
               $tmp_year =substr($rs['meterMon'],0,4);
                $this->assign('p_mon',$p_mon);
                $this->assign('mon',$tmp_mon);
                $this->assign('year',$tmp_year);	
                $this->assign('showP',$showP);
                $this->assign('showH',$showH);
                $this->assign('com',$rs);
                
                 
            }else{
                $this->error("单据不存在！");
           
            }
            $this->display();
            return;
   
       
       
       
       
       
    }
    public function recordList(){
        $bizId =  isset($_REQUEST['bizid'])?$_REQUEST['bizid']:0; 
        $meterrecord=D('MeterRecord');
        $where['bizid']=$bizId;
        $where['payflag']=0;
        $list=$meterrecord->where($where)->select();
       
        $this->assign('list',$list);
        $this->display();
    }
}
?>
